<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@include file="/quick/include/common.jsp"%>
<%@include file="/quick/include/ajaxRichGrid.jsp"%>
<%@include file="/quick/include/commonQuery.jsp"%>
<c:set var="buttons">
    <input type="button" style="margin-right: 5px"   onclick="f_add('T_COLLEGE')" value="<fmt:message key="button.add"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_update('T_COLLEGE')" value="<fmt:message key="button.update"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_cancel('T_COLLEGE')" value="<fmt:message key="button.cancel"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_save('T_COLLEGE')" value="<fmt:message key="button.save"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_query('T_COLLEGE')" value="<fmt:message key="button.query"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_year('T_COLLEGE')" value="<fmt:message key="button.year.college"/>"/>
</c:set>
<div id='toolbar' style="display:none">
<c:out value="${buttons}" escapeXml="false"/>
</div>

 
<c:set var="buttons">
	<input type="button" style="margin-right: 5px" onclick="f_addCollege()"		value="<fmt:message key="button.add"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_delete('V_YEAR_COLLEGE')"		value="<fmt:message key="button.delete"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_saveCollege()"		value="<fmt:message key="button.save"/>" />
</c:set>
<div id='subToolbar' style="display:none">
	<c:out value="${buttons}" escapeXml="false" />
</div>

<div id='year_college_div'></div>

<script>
var formSchoolYear,cascadeWin,collegeGrid;
var ycTable='V_YEAR_COLLEGE';
var curSchoolYear="<c:out value='${CURRENT_SHCOOL_YEAR}'/>";
Ext.onReady(globalScope._ready);

Ext.onReady(function(){
  collegeGrid=globalScope.grid.get("T_COLLEGE");
  var storeSchoolYear=new Ext.data.Store({//学年度数据源
    proxy:new Ext.data.HttpProxy({url:g_rootPath+"ajax/findCodes.html?categoryCode=SCHOOL_YEAR"}),
    reader:new Ext.data.ArrayReader({},[
      {name:'schoolYearCode'},
      {name:'schoolYearName'}
    ]) 
  });
  storeSchoolYear.load();  
  
  var comboSchoolYear=new Ext.form.ComboBox({//学年度下拉框
    store:storeSchoolYear,
    fieldLabel:'学年度',
    emptyText:'请选择',
    mode:'local',
    triggerAction:'all',
    valueField:'schoolYearCode',
    displayField:'schoolYearName',
    value:curSchoolYear,
    readOnly:true
  }); 
  
comboSchoolYear.on('select',function(comboBox){
    curSchoolYear=comboBox.getValue();
    globalScope._model({tableName:ycTable,SCHOOL_YEAR:curSchoolYear},function(responseText){
        var htmlDiv=Ext.get("ajaxTable_html").dom;
	    htmlDiv.innerHTML=responseText;
	    var htmlTable=Ext.get(ycTable);
	    globalScope.grid.get(ycTable).reloadData(htmlTable);	
   },false);	
}); 
     
	formSchoolYear = new Ext.form.FormPanel({
	      labelAlign:'right',
	      title:'学年度',
	      labelWidth:80,
	      region:'north',
	      height:100,
	      frame:true,
	      width:320,
	      items:[comboSchoolYear]  
	});
	
  globalScope._model({tableName:ycTable,SCHOOL_YEAR:curSchoolYear},function(responseText){
	var toolbar=new Ext.ux.toolbar('subToolbar',{});
	_ajaxRichGrid(responseText,ycTable,toolbar,'系');	
	  var subGrid=globalScope.grid.get(ycTable);
	  subGrid.region='center';
	  cascadeWin= new Ext.Window({
	    id:'c_win',
	    el:'year_college_div',
	    layout:'border',
	    title:'各学年系设置情况',
	    width:1000,
	    height:g_height*1.2,
	    closeAction:'hide',
	    buttonAlign:'center',
	    items: [formSchoolYear,subGrid],		        
	    buttons: [{
	        text:'<fmt:message key="button.close"/>',
	        tabIndex:formSchoolYear.lastTabIndex+1,
	        handler:function(){
	          globalScope.grid.add("T_COLLEGE",collegeGrid);
	          Ext.get("commonQueryTable_html").dom.innerHTML="";
	          Ext.get("T_COLLEGE");//重新读取"T_COLLEGE"对象,清除查询t_college时生成的ext缓存
	          cascadeWin.hide();
	        }
	    }]
	});	    
  }); 
  
  	  
});

function f_year(){
   cascadeWin.show();
}

function f_saveCollege(){
  globalScope._simpleSave({tableName:ycTable,saveTableName:'T_YEAR_COLLEGE',url:'quick/quickJsonProcess.html'});
}

function f_addCollege(){
  f_commonQuery('T_COLLEGE',function(recs){
	    Ext.each(recs,function(item){
		      var updtArray=[];
		      var commonQueryGrid=globalScope.grid.get(ycTable);
		      var companyDepartmentStore=commonQueryGrid.getStore();
			  var newRecord=commonQueryGrid.newRecord('newId');//新建Record
			  newRecord.copyData(item.data); //复制数据
			  newRecord.set('REFID',item.id);
			  newRecord.set('SCHOOL_YEAR',curSchoolYear);
			  commonQueryGrid.stopEditing();//插入Record
	    	  companyDepartmentStore.insert(0,newRecord); 
	    	  commonQueryGrid.startEditing(0,0);
	    	  newRecord.dirty=true;
	    	  newRecord.modified=newRecord.data;
	    	  if(companyDepartmentStore.modified.indexOf(newRecord)==-1){
	    	    companyDepartmentStore.modified.push(newRecord);	
	    	  }	
	    });	
      },
	  function(params){
        Ext.apply(params,{commonQueryCondition:'year_college_condition',placeValues:"[\""+curSchoolYear+"\"]"});	  
      }
  );
  
}

function f_add(tableName){
  globalScope._add(tableName);
}

function f_update(tableName){
  globalScope._update(tableName);
}

function f_delete(tableName){
  globalScope._delete(tableName);
} 

function f_cancel(tableName){
  globalScope._cancel(tableName);
}

function f_save(tableName,url){
  globalScope._save(tableName,url);
}

function f_query(tableName){
    globalScope._query(tableName);
}
function f_gotoPage(pageNo,rowsOfPage,pageCount,rowCount,conditoin){
  globalScope._gotoPage({spanName:"g_pageSpan_<c:out value='${model.tableName}'/>",pageNo:pageNo,rowsOfPage:rowsOfPage,pageCount:pageCount,rowCount:rowCount,condition:conditoin,tableName:"<c:out value='${model.tableName}'/>"});
}
</script>
